class Counter {
    constructor(limit) {
        this.limit = limit
    }

    [Symbol.iterator]() {
        let count = 1, limit = this.limit;
        return {
            next () {
                if (count <= limit) {
                    return { done: false, value: count++ };
                } else {
                    return { done: true, value: undefined };
                }
            },
            return () {
                console.log({msg: '提前退出'})
                return { done: true, value: undefined }
            }
        } 
    }
}

var counter = new Counter(5)
var iter = counter[Symbol.iterator]()

for (let i of counter) {
    console.log(i, iter.next())
    if (i > 2) {
        break;
    }
}

console.log('end', iter.next())